﻿using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using cludg.Web.Core.Validation;

namespace cludg.Web.ViewModels
{
	[CustomValidation(typeof(AccountModelValidator), "ValidateAccountModel")]
	public class AccountModel : EditUserModel
	{
		[Required(ErrorMessage = ValidationConst.RequiredMessage)]
		[StringLength(100, ErrorMessage = ValidationConst.StringLengthMaxInvalidMessage)]
		[DisplayName("Email")]
		[RegularExpression(ValidationConst.EmailRegEx, ErrorMessage = ValidationConst.EmailIsIvalidErrorMessage)]
		public string Email { get; set; }

		[StringLength(20, MinimumLength = 6, ErrorMessage = ValidationConst.StringLengthMaxMinInvalidMessage)]
		[DisplayName("Новый пароль")]
		public string NewPassword { get; set; }

		[StringLength(20, MinimumLength = 6, ErrorMessage = ValidationConst.StringLengthMaxMinInvalidMessage)]
		[DisplayName("И еще раз новый пароль")]
		public string NewPasswordConfirm { get; set; }

		[Required(ErrorMessage = ValidationConst.RequiredMessage)]
		[DisplayName("Текущий пароль")]
		public string CurrentPassword { get; set; }

		public bool NotEditable { get; set; }
	}

	public class AccountModelValidator
	{
		public static ValidationResult ValidateAccountModel(AccountModel model)
		{
			if (model == null)
				return ValidationResult.Success;
			if (string.IsNullOrEmpty(model.NewPassword) && string.IsNullOrEmpty(model.NewPasswordConfirm))
				return ValidationResult.Success;

			// check new pass
			if (model.NewPassword != model.NewPasswordConfirm)
				return new ValidationResult(ValidationConst.PasswordAndConfirmNotIdenticalMessage,
					new List<string> { "NewPassword", "NewPasswordConfirm" });

			return ValidationResult.Success;
		}
	}

}